$(function(){
    $("#loginButton").click(submit);
    $("body").keydown(function() {
        if (event.keyCode == "13") {
            submit();
        }
    });
});

function submit() {
  var key = "";
  $.ajax({
    type : "get",
    url : "getEncKey.do",
    data : {},
    async : false,
    dataType : "json",
    contentType : "application/x-www-form-urlencoded; charset=utf-8",
    success : function(v) {
      key = v.encKey;
    },
    error : function(v) {
      toastrComponent.show("warning", "获取密钥失败，请检查网络");
    }
  });

  if(key == ""){
    return;
  }

	var userId = strEnc($('#userId').val(), key, key, key);
	var password = strEnc($('#password').val(), key, key, key);

	var param = {
    userId : userId,
		password : password
	};
	if (userId.length <= 0) {
    toastrComponent.show("warning", "请输入用户名！");
		return false;
	} else if (password.length <= 0) {
    toastrComponent.show("warning", "请输入密码！");
		return false;
	} else {
		$.ajax({
			type : "post",
			url : "login.do",
			data : param,
      dataType : "json",
      contentType : "application/x-www-form-urlencoded; charset=utf-8",
			success : function(v) {
				if (v.result == 'index') {
					window.location.href = 'zlmanage.html';
				}else {
          toastrComponent.show("warning", v.result);
        }
			},
			error : function(v) {
        toastrComponent.show("warning", v);
			}
		});
	}
}
